import {
  userCollection
} from '../share/index'

export default async function ({
  uid,
  level = 1,
  limit = 20,
  offset = 0,
  needTotal = false
}) {
  try {
    const res = await userCollection.where({
      [`inviter_uid.${level - 1}`]: uid
    })
      .field({
        _id: true,
        username: true,
        mobile: true,
        invite_time: true
      })
      .orderBy('invite_time', 'desc')
      .skip(offset)
      .limit(limit)
      .get()
    const result = {
      code: 0,
      msg: '获取邀请列表成功',
      invitedUser: res.data
    }
    if (needTotal) {
      const totalRes = await userCollection.where({
        [`inviter_uid.${level - 1}`]: uid
      })
        .count()
      result.total = totalRes.total
    }
    return result
  } catch (error) {
    return {
      code: 90001,
      msg: '数据库读写错误'
    }
  }
}
